# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
#     https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
#     https://docs.fastlane.tools/plugins/available-plugins
#

# Uncomment the line if you want fastlane to automatically update itself
# update_fastlane

default_platform(:ios)

platform :ios do

  desc "build one adhoc release on github actions"
  lane :github_action_adhoc do
    create_keychain(
      name: ENV['MATCH_KEYCHAIN_NAME'],
      password: ENV['MATCH_PASSWORD'],
      default_keychain: true,
      unlock: true,
      timeout: 3600,
      add_to_search_list: true
    ) 
    match(type: "adhoc",
          readonly: true,
          app_identifier: "com.zm.*",
          profile_name: "*** AdHoc com.zm.*",
          keychain_name: ENV['MATCH_KEYCHAIN_NAME'],
          keychain_password: ENV['MATCH_PASSWORD'],
          git_url: ENV['MATCH_GITHUB_URL'])

    gym(workspace: "ZLGitHubClient.xcworkspace",
        scheme: "ZLGitHubClient",
        clean: true,
        include_symbols: true,
        include_bitcode: false,
        output_directory: "./fastlane/ipa/adhoc",
        output_name: "ZLGitHubClient.ipa",
        export_options: {
          method: "adhoc",
          provisioningProfiles: {
            "com.zm.ZLGitHubClient" => "*** AdHoc com.zm.*",
            "com.zm.ZLGitHubClient.Fixed-Repo" => "*** AdHoc com.zm.*"
          }
        })
  end

  desc "build one TestFlight release on github action"
  lane :gitee_action_testFlight do

    api_key = app_store_connect_api_key(
    key_id: ENV['APPSTOREAPIKEYID'],
    issuer_id: ENV['APPSTOREAPIISSUERID'],
    key_content: ENV['APPSTOREAPIKEY'],
    duration: 1200, # optional
    in_house: false, # optional but may be required if using match/sigh
    )

    create_keychain(
      name: ENV['MATCH_KEYCHAIN_NAME'],
      password: ENV['MATCH_PASSWORD'],
      default_keychain: true,
      unlock: true,
      timeout: 3600,
      add_to_search_list: true
    )

    match(type: "appstore",
          readonly: true,
          app_identifier: "com.zm.ZLGiteeClient",
          profile_name: "*** AppStore com.zm.ZLGiteeClient",
          keychain_name: ENV['MATCH_KEYCHAIN_NAME'],
          keychain_password: ENV['MATCH_PASSWORD'],
          git_url: ENV['MATCH_GITEE_URL'])

    build_num = app_store_build_number(
      app_identifier: "com.zm.ZLGiteeClient",
      live: false,
      api_key: api_key,
      initial_build_number: 1
    )

    increment_build_number(
      build_number: build_num + 1
    )

    gym(workspace: "ZLGiteeClient.xcworkspace",
        scheme: "ZLGiteeClient",
        export_method: "app-store",
        clean: true,
        include_symbols: true,
        include_bitcode: false,
        output_directory: "./fastlane/ipa/TestFlight",
        output_name: "ZLGiteeClient.ipa")

    pilot(
      api_key: api_key,
      app_identifier: "com.zm.ZLGiteeClient",
      changelog: "release to TestFlight",
      ipa: "./fastlane/ipa/TestFlight/ZLGiteeClient.ipa"
      )
  end
end
